public class StopThread2 {
    public static void main(String[] args) throws InterruptedException {
        MRunnable runnable = new MRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(1000);
        runnable.cancel();
    }

    public static class MRunnable implements Runnable {
        private int i = 0;
        private volatile boolean mStop = false;

        @Override
        public void run() {
            while (!mStop) {
                System.out.println(++i);
            }
            System.out.println("停止");
        }

        public void cancel() {
            mStop = true;
        }
    }
}
